package com.zj.base.permission.checker;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.zj.base.permission.ConfigurationModules;
import com.zj.base.permission.interfaces.OnActionClickListener;
import com.zj.base.permission.interfaces.OnPermissionCheckListener;
import com.zj.base.permission.interfaces.OnPermissionDialogCancel;
import com.zj.base.permission.interfaces.OnPermissionDialogFill;
import com.zj.base.permission.manifests.PermissionsManifest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhaojie on 2018/5/29.
 */

@SuppressWarnings({"UnusedReturnValue", "unused"})
public abstract class BasePermissionChecker {

    private OnPermissionDialogFill onPermissionDialogFill;

    OnPermissionDialogCancel onPermissionDialogCancel;

    public static final int PERMISSIONCODE = 0xaf;

    protected Activity activity;
    public Map<String, Boolean> permissionCache = new HashMap<>();
    @Nullable
    protected OnPermissionCheckListener listener;
    @Nullable
    private AlertDialog alertDialog;

    BasePermissionChecker(@NonNull Activity activity) {
        permissions = new ArrayList<>();
        successfulPermissions = new ArrayList<>();
        failedPermissions = new ArrayList<>();
        ignore = new ArrayList<>();
        this.activity = activity;
    }

    private List<ConfigurationModules> permissions;
    private List<ConfigurationModules> successfulPermissions;
    private List<ConfigurationModules> failedPermissions;
    private List<ConfigurationModules> ignore;

    protected final boolean isEnd() {
        return failedPermissions.size() == 0 && successfulPermissions.size() == permissions.size();
    }

    boolean isIgnore(ConfigurationModules module) {
        return ignore.contains(module);
    }

    public final int getCount() {
        return permissions.size();
    }

    public BasePermissionChecker setOnPermissionCheckerListener(OnPermissionCheckListener listener) {
        this.listener = listener;
        return this;
    }

    public abstract void check();

    public BasePermissionChecker setPermissions(Map<String, Boolean> maps) {
        if (maps == null || maps.size() <= 0) return this;
        List<ConfigurationModules> modules = new ArrayList<>();
        for (Map.Entry<String, Boolean> entry : maps.entrySet()) {
            permissionCache.put(entry.getKey(), true);
            ConfigurationModules module = PermissionsManifest.getAnnotationType(entry.getKey());
            if (module != null) {
                module.isForce = entry.getValue();
                modules.add(module);
            }
        }
        this.permissions = modules;
        return this;
    }

    void showAlert(boolean showForce, final ConfigurationModules module, final OnActionClickListener actionClickListener) {
        if (alertDialog != null && alertDialog.isShowing()) return;
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("授权提示：(" + (showForce ? (module.isForce ? "必要权限" : "可选权限") : "") + ")");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (onPermissionDialogCancel != null)
                    onPermissionDialogCancel.onNegative(dialog, module);
                else if (actionClickListener != null)
                    actionClickListener.cancel(dialog);

            }
        });
        builder.setPositiveButton("重新授权", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (onPermissionDialogFill != null)
                    onPermissionDialogFill.onPositive(dialog, module);
                else if (actionClickListener != null)
                    actionClickListener.fulfil(dialog);
            }
        });
        builder.setCancelable(false);
        builder.setMessage(module.detail);
        alertDialog = builder.create();
        alertDialog.show();
    }


    List<ConfigurationModules> getPermissions() {
        return permissions;
    }

    public ConfigurationModules getPermission(int i) {
        return permissions.get(i);
    }

    void addSuccess(ConfigurationModules module) {
        if (!isExists(successfulPermissions, module)) successfulPermissions.add(module);
        if (isExists(failedPermissions, module)) failedPermissions.remove(module);
    }

    void addFail(ConfigurationModules module) {
        if (!isExists(failedPermissions, module)) failedPermissions.add(module);
        if (isExists(successfulPermissions, module)) successfulPermissions.remove(module);
    }

    void addIgnore(ConfigurationModules module) {
        ignore.add(module);
        addSuccess(module);
    }

    ConfigurationModules getFailPermission(int i) {
        return failedPermissions.get(i);
    }

    private boolean isExists(List<ConfigurationModules> lst, ConfigurationModules module) {
        return lst.contains(module);
    }

    public void setOnPermissionDialogFill(OnPermissionDialogFill onPermissionDialogFill) {
        this.onPermissionDialogFill = onPermissionDialogFill;
    }

    public void setOnPermissionDialogCancel(OnPermissionDialogCancel onPermissionDialogCancel) {
        this.onPermissionDialogCancel = onPermissionDialogCancel;
    }

    public void clear() {
        if (ignore != null) {
            ignore.clear();
            ignore = null;
        }
        if (permissions != null) {
            permissions.clear();
            permissions = null;
        }
        if (failedPermissions != null) {
            failedPermissions.clear();
            failedPermissions = null;
        }
        if (successfulPermissions != null) {
            successfulPermissions.clear();
            successfulPermissions = null;
        }
        if (alertDialog != null) {
            if (alertDialog.isShowing()) alertDialog.dismiss();
            alertDialog = null;
        }
        if (activity != null) {
            activity = null;
        }
    }
}
